﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.Collections;
using DevExpress.XtraGrid.Columns;
using DXApplicationTangche.UC.订单.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;

namespace DXApplicationTangche.UC.订单
{
    public partial class Form订单检索 : DevExpress.XtraEditors.XtraForm
    {
        public List<OrderCommitDTO返回> OrderCommitDTO返回s = new List<OrderCommitDTO返回>();
        public List<DTO订单检索条件> DTO订单检索条件s = new List<DTO订单检索条件>();
        private GridEditorCollection gridEditors;
        private int curPage = 1;
        public Form订单检索()
        {
            InitializeComponent();
            this.DTO订单检索条件s = new 订单检索Service().DTO订单检索条件s;
            this.gridEditors = new GridEditorCollection();
            InitInplaceEditors();
            this.gridControl检索条件.DataSource = gridEditors;
            this.xtraUserControl分页.williamPagerEvent += this.refreshGrid;
        }
        #region
        private void InitInplaceEditors()
        {
            foreach (DTO订单检索条件 item in DTO订单检索条件s)
            {
                if (item.fuzzyQuery)
                {
                    this.gridEditors.Add(this.repositoryItemTextEdit1, item.displayName, item.queryText, item.queryItemMatch, item.queryName);
                }
                else{
                    this.gridEditors.Add(new RepositoryItemComboBox下拉框(item.optionValues),item.displayName, new RepositoryItemComboBox下拉框(item.optionValues), item.queryItemMatch,item.queryName);
                }
            }
            
        }
        
        private void gridView检索条件_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumnValue)
            {
                GridEditorItem item = gridView检索条件.GetRow(e.RowHandle) as GridEditorItem;
                if (item != null) e.RepositoryItem = item.RepositoryItem;
            }
        }
        public class GridEditorItem
        {
            string fName;
            object fValue;
            Boolean fQueryItemMatch;
            String fQueryName;
            RepositoryItem fRepositoryItem;
            public GridEditorItem(RepositoryItem fRepositoryItem, string fName, object fValue, Boolean fQueryItemMatch, string fQueryName)
            {
                this.fRepositoryItem = fRepositoryItem;
                this.fName = fName;
                this.fValue = fValue;
                this.fQueryItemMatch = fQueryItemMatch;
                this.fQueryName = fQueryName;
            }
            public string Name { get { return this.fName; } }
            public object Value { get { return this.fValue; } set { this.fValue = value; } }
            public Boolean queryItemMatch { get { return this.fQueryItemMatch; } set { this.fQueryItemMatch = value; } }
            public string queryName { get { return this.fQueryName; } set { this.fQueryName = value; } }
            public RepositoryItem RepositoryItem { get { return this.fRepositoryItem; } }
        }
        class GridEditorCollection : ArrayList
        {
            public GridEditorCollection()
            {
            }
            public new GridEditorItem this[int index] { get { return base[index] as GridEditorItem; } }
            public void Add(RepositoryItem fRepositoryItem, string fName, object fValue, Boolean fQueryItemMatch, string fQueryName)
            {
                base.Add(new GridEditorItem(fRepositoryItem, fName, fValue, fQueryItemMatch ,fQueryName));
            }
        }
        #endregion
        private void gridView检索条件_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column == this.gridColumnValue)
            {
                this.DTO订单检索条件s[e.RowHandle].queryText = e.Value.ToString();
            }
            else if (e.Column == this.gridColumnqueryItemMatch)
            {
                this.DTO订单检索条件s[e.RowHandle].queryItemMatch = Convert.ToBoolean(e.Value.ToString());
            }
        }
        private void simpleButton检索_Click(object sender, EventArgs e)
        {
            this.refreshGrid(this.curPage);
        }
        private void refreshGrid(int curPage)
        {
            this.curPage = curPage;
            this.OrderCommitDTO返回s = new 订单检索Service(this.DTO订单检索条件s, (curPage - 1).ToString(), "100").OrderCommitDTO返回s;
            this.gridControl订单.DataSource = this.OrderCommitDTO返回s;
            this.bandedGridView订单.RefreshData();
            this.bandedGridView订单.ExpandAllGroups();
        }
    }
}